package com.orangelife.service.propFeeReceipt.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.cerHouseholder.ICerHouseholderService;
import com.orangelife.service.propFeeBillLog.IPropFeeBillLogService;
import com.orangelife.service.propFeeBillLog.model.PropFeeBillLogDto;
import com.orangelife.service.propFeeDirectBill.IPropFeeDirectBillService;
import com.orangelife.service.propFeeDirectBill.model.PropFeeDirectBillDto;
import com.orangelife.service.propFeeOffsetInfo.IPropFeeOffsetInfoService;
import com.orangelife.service.propFeePreBill.IPropFeePreBillService;
import com.orangelife.service.propFeePreBill.model.PropFeePreBillDto;
import com.orangelife.service.propFeeReceipt.IPropFeeReceiptService;
import com.orangelife.service.propFeeReceipt.model.PropFeeReceiptDto;
import com.orangelife.service.propFeeReceiptLink.IPropFeeReceiptLinkService;
import com.orangelife.service.propFeeReceiptLink.model.PropFeeReceiptLinkDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.DateStyle;
import com.orangelife.utils.DateUtil;
import com.orangelife.utils.HttpSvcUtils;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.JsonMapUtils;
import com.orangelife.utils.StringUtils;
import com.orangelife.utils.UrlPath;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeReceipt类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-12-12 15:47:46
 * ==============================================================
 */
@Service("propFeeReceiptService")
public class PropFeeReceiptServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeReceiptService {
	private static Logger logger = Logger
			.getLogger(PropFeeReceiptServiceImpl.class);
	@Resource(name = "propFeeReceiptLinkService")
	private IPropFeeReceiptLinkService propFeeReceiptLinkService;
	@Resource(name = "propFeeBillLogService")
	private IPropFeeBillLogService propFeeBillLogService;
	@Resource(name = "propFeePreBillService")
	private IPropFeePreBillService propFeePreBillService;
	@Resource(name = "propFeeDirectBillService")
	private IPropFeeDirectBillService propFeeDirectBillService;
	@Resource(name = "propFeeOffsetInfoService")
	private IPropFeeOffsetInfoService propFeeOffsetInfoService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeReceiptServiceImpl.logger = logger;
	}

	@Resource(name = "cerHouseholderService")
	private ICerHouseholderService cerHouseholderService;

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeReceipt(PropFeeReceiptDto propFeeReceipt)
			throws AppBizException {

		Assert.notNull(propFeeReceipt);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeReceipt.getReceiptNum())) {
			if (propFeeReceipt.getReceiptNum().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "收据编码长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getReceiptType())) {
			if (propFeeReceipt.getReceiptType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"收据类型(1预存款，2其他费用，3转存，4冲抵)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getStatus())) {
			if (propFeeReceipt.getStatus().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"状态(1有效，2报废)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getHouseInfo())) {
			if (propFeeReceipt.getHouseInfo().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "房屋信息长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getDrawer())) {
			if (propFeeReceipt.getDrawer().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "开票人长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getAmountInWords())) {
			if (propFeeReceipt.getAmountInWords().length() > 64) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "大写金额长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getAmount())) {
			if (propFeeReceipt.getAmount().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "金额长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getRemark())) {
			if (propFeeReceipt.getRemark().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getContent())) {
			if (propFeeReceipt.getContent().length() > 2560) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"票据内容json格式长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getCreateuser())) {
			if (propFeeReceipt.getCreateuser().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "创建者长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getEdituser())) {
			if (propFeeReceipt.getEdituser().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		propFeeReceipt.setReceiptID(IdUtil.getuuId());
		propFeeReceipt.setItemStat(TypeConst.FLAG_ON);
		propFeeReceipt.setTscreate(new Date());
		propFeeReceipt.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeReceiptMapper.insertSelective", propFeeReceipt);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeReceipt(PropFeeReceiptDto propFeeReceipt)
			throws AppBizException {

		Assert.notNull(propFeeReceipt);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeReceipt.getReceiptID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeReceiptDto oldPropFeeReceipt = this
				.findPropFeeReceiptByID(propFeeReceipt.getReceiptID());

		if (!StringUtils.isBlank(propFeeReceipt.getReceiptNum())) {
			if (propFeeReceipt.getReceiptNum().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "收据编码长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getReceiptType())) {
			if (propFeeReceipt.getReceiptType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"收据类型(1预存款，2其他费用，3转存，4冲抵)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getStatus())) {
			if (propFeeReceipt.getStatus().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"状态(1有效，2报废)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getHouseInfo())) {
			if (propFeeReceipt.getHouseInfo().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "房屋信息长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getDrawer())) {
			if (propFeeReceipt.getDrawer().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "开票人长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getAmountInWords())) {
			if (propFeeReceipt.getAmountInWords().length() > 64) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "大写金额长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getAmount())) {
			if (propFeeReceipt.getAmount().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "金额长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getRemark())) {
			if (propFeeReceipt.getRemark().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getContent())) {
			if (propFeeReceipt.getContent().length() > 2560) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"票据内容json格式长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getCreateuser())) {
			if (propFeeReceipt.getCreateuser().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "创建者长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeReceipt.getEdituser())) {
			if (propFeeReceipt.getEdituser().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		if (oldPropFeeReceipt == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeReceipt.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeReceiptMapper.updateByPrimaryKeySelective",
				propFeeReceipt);
	}

	@Override
	public PropFeeReceiptDto findPropFeeReceiptByID(String propFeeReceiptID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeReceiptID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeReceiptDto oldPropFeeReceipt = this.getSqlSession().selectOne(
				"PropFeeReceiptSearchMapper.selectByPrimaryKey",
				propFeeReceiptID);
		if (oldPropFeeReceipt == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeReceipt.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeReceipt;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeReceiptByLogic(String propFeeReceiptID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeReceiptID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeReceiptDto oldPropFeeReceipt = this
				.findPropFeeReceiptByID(propFeeReceiptID);

		oldPropFeeReceipt.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeReceipt.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeReceiptMapper.updateByPrimaryKeySelective",
				oldPropFeeReceipt);
	}

	@Override
	public List<Map<String, Object>> findPropFeeReceiptByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeReceiptSearchMapper.findAllPropFeeReceipt", map);
		return list;
	}

	@Override
	public List<PropFeeReceiptDto> getPropFeeReceiptByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeReceiptDto> list = this.getSqlSession().selectList(
				"PropFeeReceiptSearchMapper.getAllPropFeeReceiptDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeReceiptByRelevantID(
			String receiptID, String receiptType, String status, String pmID,
			String commID, String apartmentID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("receiptID", receiptID);
		map.put("receiptType", receiptType);
		map.put("status", status);
		map.put("pmID", pmID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeReceiptSearchMapper.selectPropFeeReceiptDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeReceiptDto> findPropFeeReceiptByRelevantID(
			String receiptID, String receiptType, String status, String pmID,
			String commID, String apartmentID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("receiptID", receiptID);
		map.put("receiptType", receiptType);
		map.put("status", status);
		map.put("pmID", pmID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);

		List<PropFeeReceiptDto> list = this.getSqlSession().selectList(
				"PropFeeReceiptSearchMapper.findPropFeeReceiptDtoByRelevantID",
				map);
		return list;
	}

	@Override
	public List<PropFeeReceiptDto> bauthfindPropFeeReceipt(
			List<String> propFeeReceiptIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeReceiptDto> list = this.getSqlSession().selectList(
				"PropFeeReceiptSearchMapper.batchFindPropFeeReceipt",
				propFeeReceiptIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeReceiptByMap(
			Map<String, Object> propFeeReceipt) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeReceiptSearchMapper.batchFindPropFeeReceiptByMap",
				propFeeReceipt);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public Map<String, Object> addPropFeeReceipts(
			PropFeeReceiptDto propFeeReceipt) throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();
		if (propFeeReceipt.getReceiptLinkIDs() == null
				|| propFeeReceipt.getReceiptLinkIDs().size() <= 0) {
			throw new AppBizException(2, "亲，关联信息");
		}
		if (StringUtils.isBlank(propFeeReceipt.getReceiptType())) {
			throw new AppBizException(2, "亲，票据类型");
		}
		if (StringUtils.isBlank(propFeeReceipt.getCommID())) {
			throw new AppBizException(2, "亲，小区信息");
		}
		if (StringUtils.isBlank(propFeeReceipt.getApartmentID())) {
			throw new AppBizException(2, "亲，房屋信息");
		}
		if (StringUtils.isBlank(propFeeReceipt.getReceiptNum())) {
			throw new AppBizException(2, "亲，收据编码");
		}
		if (StringUtils.isBlank(propFeeReceipt.getAmount())) {
			throw new AppBizException(2, "亲，金额");
		}
		if (StringUtils.isBlank(propFeeReceipt.getAmountInWords())) {
			throw new AppBizException(2, "亲，大写金额");
		}
		if (!StringUtils.isBlank(propFeeReceipt.getApartmentID())) {
			Map<String, Object> propFeeComm = cerHouseholderService
					.getHouseInfo(propFeeReceipt.getApartmentID());
			StringBuffer houseInfo = new StringBuffer();
			houseInfo.append(propFeeComm.get("buildingName") + "-");
			if (propFeeComm.get("unitName") != null) {
				houseInfo.append(propFeeComm.get("unitName")).append("-");
			}
			houseInfo.append(propFeeComm.get("apartmentNum"));

			propFeeReceipt.setHouseInfo(houseInfo.toString());
		}
		propFeeReceipt.setStatus("1");

		propFeeReceipt.setReceiptTime(new Date());
		int n = this.addPropFeeReceipt(propFeeReceipt);
		List<PropFeeReceiptLinkDto> propFeeReceiptLinkDtos = new ArrayList<PropFeeReceiptLinkDto>();
		if (n > 0) {
			for (int i = 0; i < propFeeReceipt.getReceiptLinkIDs().size(); i++) {
				List<PropFeeReceiptLinkDto> propFeeReceiptLins = propFeeReceiptLinkService
						.findPropFeeReceiptLinkByRelevantID(null, null,
								propFeeReceipt.getReceiptType(), propFeeReceipt
										.getReceiptLinkIDs().get(i));
				if (propFeeReceiptLins != null && propFeeReceiptLins.size() > 0) {
					throw new AppBizException(2, "亲，该信息已经操作过");
				}
				PropFeeReceiptLinkDto propFeeReceiptLinkDto = new PropFeeReceiptLinkDto();
				propFeeReceiptLinkDto.setItemStat("1");
				propFeeReceiptLinkDto.setLinkID(propFeeReceipt
						.getReceiptLinkIDs().get(i));
				propFeeReceiptLinkDto.setReceiptID(propFeeReceipt
						.getReceiptID());
				propFeeReceiptLinkDto.setReceiptLinkID(IdUtil.getuuId());
				propFeeReceiptLinkDto.setTscreate(new Date());
				propFeeReceiptLinkDto.setType(propFeeReceipt.getReceiptType());
				propFeeReceiptLinkDtos.add(propFeeReceiptLinkDto);
			}
			propFeeReceiptLinkService
					.addPropFeeReceiptLink(propFeeReceiptLinkDtos);
		}
		map.put("propFeeReceipt", propFeeReceipt);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("type", "1");
		paramMap.put("commID", propFeeReceipt.getCommID());
		paramMap.put("receiptNum", propFeeReceipt.getReceiptNum());
		paramMap.put("useStatus", "2");
		paramMap.put("payType", propFeeReceipt.getPayType());
		JsonMapUtils.getEntityMap(HttpSvcUtils.doPutSvc(
				UrlPath.editReceiptNumStore, paramMap,
				propFeeReceipt.getAccessToken()));
		return map;
	}

	@Override
	public PropFeeReceiptDto findPropFeeReceipt(String linkID, String type)
			throws AppBizException {
		List<PropFeeReceiptLinkDto> propFeeReceiptLins = propFeeReceiptLinkService
				.findPropFeeReceiptLinkByRelevantID(null, null, type, linkID);
		if (propFeeReceiptLins != null && propFeeReceiptLins.size() > 0) {
			return this.findPropFeeReceiptByID(propFeeReceiptLins.get(0)
					.getReceiptID());
		}
		return null;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public Map<String, Object> nullifyReceipt(PropFeeReceiptDto propFeeReceipt)
			throws AppBizException {
		PropFeeReceiptDto oldFeeReceiptDto = this
				.findPropFeeReceiptByID(propFeeReceipt.getReceiptID());
		propFeeReceipt.setStatus("2");
		this.editPropFeeReceipt(propFeeReceipt);

		List<PropFeeReceiptLinkDto> propFeeReceiptLinkDtos = propFeeReceiptLinkService
				.findPropFeeReceiptLinkByRelevantID(null,
						propFeeReceipt.getReceiptID(),
						oldFeeReceiptDto.getReceiptType(), null);

		List<String> linkIDs = new ArrayList<String>();
		for (PropFeeReceiptLinkDto propFeeReceiptLinkDto : propFeeReceiptLinkDtos) {
			linkIDs.add(propFeeReceiptLinkDto.getReceiptLinkID());

		}

		if (linkIDs != null && linkIDs.size() > 0) {
			this.getSqlSession().update(
					"PropFeeReceiptLinkBauthMapper.bauthDeleteLg", linkIDs);
		}

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("type", "1");
		paramMap.put("commID", oldFeeReceiptDto.getCommID());
		paramMap.put("receiptNum", oldFeeReceiptDto.getReceiptNum());
		paramMap.put("useStatus", "3");

		return JsonMapUtils.getEntityMap(HttpSvcUtils.doPutSvc(
				UrlPath.editReceiptNumStore, paramMap,
				propFeeReceipt.getAccessToken()));

	}

	@Override
	public List<Map<String, Object>> findPropFeeReceiptInfo(String type,
			String apartmentID, String tsCreate, String search, int pageNo,
			int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		map.put("apartmentID", apartmentID);
		if (type.equals("1")) {
			PageHelper.startPage(pageNo, pageSize);
			List<Map<String, Object>> list = this.getSqlSession().selectList(
					"PropFeeReceiptSearchMapper.getNoReNumDetail", map);
			return list;
		}
		if (type.equals("2")) {
			PageHelper.startPage(pageNo, pageSize);
			List<Map<String, Object>> list = this.getSqlSession().selectList(
					"PropFeeReceiptSearchMapper.getReNumDetail", map);
			return list;
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getMonthNoReNum(String rType, String type,
			String apartmentID, String feeDate, String tsCreate, String search,
			int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		map.put("apartmentID", apartmentID);
		map.put("feeDate", feeDate);
		map.put("type", rType);
		if (type.equals("2")) {
			if (StringUtils.isBlank(rType)) {
				map.put("type", "4");
			}
			PageHelper.startPage(pageNo, pageSize);
			List<Map<String, Object>> list = this.getSqlSession().selectList(
					"PropFeeReceiptSearchMapper.getMonthReNum", map);
			return list;
		}
		if (type.equals("1")) {
			List<String> feeDates = propFeeOffsetInfoService
					.getOffFeeDate(apartmentID);

			map.put("feeDates", feeDates);
			PageHelper.startPage(pageNo, pageSize);
			List<Map<String, Object>> list = this.getSqlSession().selectList(
					"PropFeeReceiptSearchMapper.getMonthNoReNum", map);
			return list;
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getMonthReDetail(
			Map<String, Object> propFeeReceipt) throws AppBizException {
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeReceiptSearchMapper.getMonthReDetail", propFeeReceipt);
		return list;
	}

	@Override
	public int editPayWay(PropFeeReceiptDto propFeeReceipt)
			throws AppBizException {
		PropFeeBillLogDto propFeeBillLogDto = new PropFeeBillLogDto();
		PropFeePreBillDto preBillDto = new PropFeePreBillDto();
		PropFeeDirectBillDto directBilDto = new PropFeeDirectBillDto();
		if (propFeeReceipt.getType().equals("1")) {

			preBillDto = propFeePreBillService
					.findPropFeePreBillByID(propFeeReceipt.getLinkID());
			preBillDto.setAccessToken(propFeeReceipt.getAccessToken());
			propFeeBillLogDto.setContent(JSONObject.toJSONString(preBillDto));
			preBillDto.setEdituserID(propFeeReceipt.getCreateuserID());
			preBillDto.setPayType(propFeeReceipt.getPayType());
			String payType = getPayType(preBillDto.getPayType());
			propFeePreBillService.editPropFeePreBill(preBillDto);
			propFeeBillLogDto.setAfContent(JSONObject.toJSONString(preBillDto));
			propFeeBillLogDto.setOperation("2");
			propFeeBillLogDto.setType("1");

			propFeeBillLogDto.setRemark(propFeeReceipt.getCreateuser()
					+ "在"
					+ DateUtil.DateToString(new Date(),
							DateStyle.YYYY_MM_DD_HH_MM_SS_EN) + "变更流水号  "
					+ preBillDto.getPreBillID() + " 的 支付方式(" + payType + ")");
			preBillDto.setRemark(propFeeBillLogDto.getRemark());
		}
		if (propFeeReceipt.getType().equals("2")) {
			directBilDto = propFeeDirectBillService
					.findPropFeeDirectBillByID(propFeeReceipt.getLinkID());
			directBilDto.setAccessToken(propFeeReceipt.getAccessToken());
			propFeeBillLogDto.setContent(JSONObject.toJSONString(directBilDto));
			directBilDto.setEdituserID(propFeeReceipt.getCreateuserID());
			directBilDto.setPayType(propFeeReceipt.getPayType());
			String payType = getPayType(directBilDto.getPayType());
			propFeeDirectBillService.editPropFeeDirectBill(directBilDto);

			propFeeBillLogDto.setAfContent(JSONObject
					.toJSONString(directBilDto));
			propFeeBillLogDto.setOperation("2");
			propFeeBillLogDto.setType("2");
			propFeeBillLogDto.setRemark(propFeeReceipt.getCreateuser()
					+ "在"
					+ DateUtil.DateToString(new Date(),
							DateStyle.YYYY_MM_DD_HH_MM_SS_EN) + "变更流水号  "
					+ directBilDto.getDirectBillID() + " 的 支付方式(" + payType
					+ ")");
			preBillDto.setRemark(propFeeBillLogDto.getRemark());

		}
		propFeeBillLogDto.setCreateuserID(propFeeReceipt.getCreateuserID());
		propFeeBillLogDto.setParameter(JSONObject.toJSONString(propFeeReceipt));
		int n = propFeeBillLogService.addPropFeeBillLog(propFeeBillLogDto);

		if (n > 0) {
			/*
			 * Map<String, Object> paramMap = new HashMap<String, Object>();
			 * paramMap.put("commID", propFeeReceipt.getCommID());
			 * paramMap.put("receiptNum", propFeeReceipt.getReceiptNum());
			 * paramMap.put("payType", propFeeReceipt.getPayType());
			 * JsonMapUtils.getEntityMap(HttpSvcUtils.doPutSvc(
			 * UrlPath.editReceiptNumStore, paramMap,
			 * propFeeReceipt.getAccessToken()));
			 */

			if (propFeeReceipt.getType().equals("1")) {
				final PropFeePreBillDto preBill = preBillDto;
				Thread thread = new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							propFeePreBillService.sendInfoTozEditFinal(preBill);
						} catch (AppBizException e) {
							logger.error(e.getMessage());
							// e.printStackTrace();
						}
					}
				});
				thread.start();
			}
			if (propFeeReceipt.getType().equals("2")) {
				final PropFeeDirectBillDto directBil = directBilDto;
				Thread thread = new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							propFeeDirectBillService
									.sendInfoToEditFinal(directBil);
						} catch (AppBizException e) {
							logger.error(e.getMessage());
							// TODO Auto-generated catch block
							// e.printStackTrace();
						}
					}
				});
				thread.start();
			}

		}
		return n;
	}

	public String getPayType(String payWay) {
		String payType = "未知";
		switch (payWay) {
		case "0":
			payType = "现金";
			break;
		case "1":
			payType = "刷卡";
			break;
		case "2":
			payType = "银行转账";
			break;
		case "3":
			payType = "橙子生活";
			break;

		default:
			payType = "未知";
			break;
		}
		return payType;
	}

	@Override
	public List<Map<String, Object>> findHasUseReceiptNum(
			Map<String, Object> propFeeReceipt, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeReceiptSearchMapper.findHasUseReceiptNum",
				propFeeReceipt);
		return list;

	}

	@Override
	public Map<String, Object> invalidReceipt(PropFeeReceiptDto propFeeReceipt)
			throws AppBizException {
		PropFeeReceiptDto oldFeeReceiptDto = this
				.findPropFeeReceiptByID(propFeeReceipt.getReceiptID());
		propFeeReceipt.setItemStat("0");
		this.editPropFeeReceipt(propFeeReceipt);
		List<PropFeeReceiptLinkDto> propFeeReceiptLinkDtos = propFeeReceiptLinkService
				.findPropFeeReceiptLinkByRelevantID(null,
						propFeeReceipt.getReceiptID(),
						oldFeeReceiptDto.getReceiptType(), null);
		List<String> linkIDs = new ArrayList<String>();
		for (PropFeeReceiptLinkDto propFeeReceiptLinkDto : propFeeReceiptLinkDtos) {
			linkIDs.add(propFeeReceiptLinkDto.getReceiptLinkID());
		}
		if (linkIDs != null && linkIDs.size() > 0) {
			this.getSqlSession().update(
					"PropFeeReceiptLinkBauthMapper.bauthDeleteLg", linkIDs);
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("type", "1");
		paramMap.put("commID", oldFeeReceiptDto.getCommID());
		paramMap.put("receiptNum", oldFeeReceiptDto.getReceiptNum());
		paramMap.put("useStatus", "3");
		return JsonMapUtils.getEntityMap(HttpSvcUtils.doPutSvc(
				UrlPath.invalidReceiptNumStore, paramMap,
				propFeeReceipt.getAccessToken()));

	}

	@Override
	public List<Map<String, Object>> bauthpropFeeReceipt(
			Map<String, Object> propFeeReceipt) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeReceiptSearchMapper.bauthpropFeeReceipt",
				propFeeReceipt);
		return list;

	}
}